/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.service.impl;


import vip.dexian.admin.dao.OperateLogDao;
import vip.dexian.admin.dto.OperateLogDTO;
import vip.dexian.admin.entity.OperateLog;
import vip.dexian.admin.service.OperateLogService;
import vip.dexian.common.utils.CollectionUtils;
import vip.dexian.core.base.service.BaseServiceImpl;
import vip.dexian.core.exception.ServiceValidationException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 操作日志业务逻辑处理实现
 *
 * @author 挺好的 2023年06月07日 11:32:34
 */
@Service ("operateLogServiceImpl")
@Slf4j
public class OperateLogServiceImpl extends BaseServiceImpl <OperateLog, OperateLogDTO, Long>
        implements OperateLogService {

    /**
     * 操作日志Dao
     */
    @Resource (name = "operateLogDaoImpl")
    private OperateLogDao operateLogDao;

    /**
     * 操作日志保存
     *
     * @param bean
     *         操作日志
     */
    @Async
    @Override
    @Transactional (rollbackFor = Exception.class)
    public void log (OperateLog bean) {
        super.save(bean);
    }

    /**
     * 根据id批量删除操作日志
     *
     * @param ids
     *         id列表
     */
    @Override
    @Transactional (rollbackFor = Exception.class)
    public void delete (Long... ids) {

        log.info("delete parameter:{}", ids);

        this.validDelete(ids);

        List <Long> distinctIds = CollectionUtils.distinct(ids);

        for (Long id : distinctIds) {
            OperateLog operateLog = this.operateLogDao.findByValidationIfNull(id, "操作日志");
            if (Boolean.FALSE.equals(operateLog.getIsAllowedDelete())) {
                throw new ServiceValidationException("A50001");
            }

            super.delete(id);
        }
    }
}
